<template>
	<view>
		<view>
			 <view>
				<view class='header'>
					<image src='/static/wx_login.png'></image>
				</view>
				<view class='content'>
					<view>申请获取以下权限</view>
					<text>获得你的公开信息(昵称，头像、地区等)</text>
					<text>获得你微信绑定的手机号</text>
				</view>
				
				<!-- <button class='bottom' type='primary' open-type="getUserInfo" withCredentials="true" lang="zh_CN" @getuserinfo="wxGetUserInfo">
					授权登录
				</button> -->
				
				<button class='bottom' type='primary' open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
					授权登录
				</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {wxlogin, getPhone, wxUserInfo} from "@/store/wxlogin.js"
	export default {
		data() {
			return {
				userInfo:{
					avatarUrl: '',
					nickName: '',
					phone: ''
				},
				phoneParams:{ 
					sessionkey:"",
					ivdata:"",
					encrypdata:""
				},
				loginStatus:false,
				phoneStatus:false,
				pages: '',//跳转页面
			}
		},
		onLoad(e) {
			try{
				if(e.pages){
					this.pages = JSON.parse(decodeURIComponent(e.pages));
				}
				this.init();
			}catch(e){
				console.log("init错误信息：",e)
			}
		},
		methods: {
			async init(){
				var code = await wxlogin();
				console.log("code：",code);
				// 获取sessionkey
				var data = await this.$requestApi.getSessionkey(code)
				console.log("data：",data);
				this.phoneParams.sessionkey = data.result.session_key;
			},
			//授权获取微信用户信息
			async wxGetUserInfo(){
				var info = await wxUserInfo()
				console.log("用户信息：",info);
				this.loginStatus = true
				this.reLaunch()
			},
			//授权获取手机号
			async getPhoneNumber(e){
				this.phoneParams.encrypdata = e.detail.encryptedData
				this.phoneParams.ivdata = e.detail.iv
				console.log("phoneParams：",this.phoneParams);
				
				var info = await wxUserInfo()
				this.userInfo.avatarUrl = info.userInfo.avatarUrl;
				this.userInfo.nickName = info.userInfo.nickName;
				
				var phoneData = await this.$requestApi.getPhone(this.phoneParams)
				this.userInfo.phone = JSON.parse(phoneData.result).purePhoneNumber;
				
				console.log("用户数据:",this.userInfo)
				if(this.userInfo){
					this.reLaunch()
				}
			},
			
			reLaunch(){
				uni.setStorageSync("ys_user_info",JSON.stringify(this.userInfo))
				console.log("跳转页面:",this.pages)
				//信息更新成功后跳转页面
				if(this.pages){
					uni.reLaunch({
						url: ''+ this.pages +''
					});
				}else{
					uni.reLaunch({
						url: '/pages/index'
					});
				}
			},
			// 点击左上角返回按钮时触发事件
			goBack() {
			  // 通过判断当前页面的页面栈信息，是否有上一页进行返回，如果没有则跳转到首页
			  const pages = getCurrentPages()
			  console.log("是否有上一页进行返回:",pages)
			  if (pages && pages.length > 0) {
			    const firstPage = pages[0]
			    if (pages.length == 1 && (!firstPage.route || firstPage.route != 'pages/index')) {
			      uni.reLaunch({
			        url: '/pages/index'
			      })
			    } else {
			      uni.navigateBack({
			        delta: 1
			      })
			    }
			  } else {
			    uni.reLaunch({
			      url: '/pages/index'
			    })
			  }
			},	
		}
	}
</script>

<style>
	.header {
	    margin: 90rpx 0 50rpx 50rpx;
	    border-bottom: 1px solid #ccc;
	    text-align: center;
	    width: 650rpx;
	    height: 300rpx;
	    line-height: 450rpx;
	}
	.header image {
	    width: 200rpx;
	    height: 200rpx;
	}
	.content {
	    margin-left: 50rpx;
	    margin-bottom: 50rpx;
	}
	.content text {
	    display: block;
	    color: #9d9d9d;
	    margin-top: 40rpx;
	}
	.bottom {
	    border-radius: 80rpx;
	    margin: 35rpx 50rpx;
	    font-size: 35rpx;
	}
	.bottom:first-child{
		margin-top: 50rpx;
	}
	
	.view_input{
		margin: 0 50rpx;
		background-color: white;
		padding: 10px;
		height: 1rem;
		line-height: 1rem;
	}
</style>